from typing import List


class Solution:
    def smallestDistancePair(self, nums: List[int], k: int) -> int:
        nums.sort()
        left, right = 0, nums[-1] - nums[0]
        while left < right:
            mid = (left + right) // 2
            count = 0

            i, j = 0, 1
            while i < len(nums) and j < len(nums):
                while nums[j] - nums[i] > mid:
                    i += 1
                count += j - i
                j += 1
            if count < k:
                left = mid + 1
            else:
                right = mid
        return left